# !/usr/bin/env python
# -*- coding: utf-8 -*-
# ------------------------------
'''
2. 两数相加
给你两个非空的链表，表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的，并且每个节点只能存储 一位 数字。
请你将两个数相加，并以相同形式返回一个表示和的链表。
你可以假设除了数字 0 之外，这两个数都不会以 0 开头。
'''
class ListNode(object):
    def __init__(self, val):
        self.val = val
        self.next = None
class Solution1(object):
    def __init__(self, v1):
        self.val = v1
        self.next = None
class Solution2(object):
    def __init__(self, v2):
        self.val = v2
        self.next = None
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        flag = 0
        ints = 0
        add1 = 0
        while l1 or l2:
            # len（l2） > len(l1)
            if l1 is None:
                tem = l2.val + ints
                if tem < 10:
                    add1 += 1
                while tem >= 10 or ints >= 10:
                    ints = tem // 10
                    tem = tem % 10
                next_ln = ListNode(tem)
                if add1 >= 1:
                    ints = 0
                    add1 = 0
                tem_l.next = next_ln
                tem_l = tem_l.next
                l2 = l2.next
            # len（l1）> len(l2)
            elif l2 is None:
                tem = l1.val + ints
                if tem < 10:
                    add1 += 1
                while tem >= 10 or ints >= 10:
                    ints = tem // 10
                    tem = tem % 10
                next_ln = ListNode(tem)
                if add1 >= 1:
                    ints = 0
                    add1 = 0
                tem_l.next = next_ln
                tem_l = tem_l.next
                l1 = l1.next
            # 同长度列表
            else:
                tem = l1.val + l2.val + ints
                if tem < 10:
                    add1 += 1
                while tem >= 10 or ints >= 10:
                    ints = tem // 10
                    tem = tem % 10
                if flag == 0:
                    l3 = ListNode(tem)
                    tem_l = l3
                    add1 = 0
                else:
                    next_ln = ListNode(tem)
                    if add1 >= 1:
                        ints = 0
                        add1 = 0
                    tem_l.next = next_ln
                    tem_l = tem_l.next
                l1 = l1.next
                l2 = l2.next
            flag += 1
        if ints != 0:
            next_ln = ListNode(ints)
            tem_l.next = next_ln
            tem_l = tem_l.next
        return l3
if __name__ == '__main__':
    data1 = [8, 3, 2]
    data2 = [9, 2, 1]
    solution1 = Solution1(data1[0])
    solution2 = Solution2(data2[0])
    tmp1 = solution1
    tmp2 = solution2
    for i in data1[1:]:
        next_sulo1 = Solution1(i)
        tmp1.next = next_sulo1
        tmp1 = tmp1.next
    for j in data2[1:]:
        next_sulo2 = Solution2(j)
        tmp2.next = next_sulo2
        tmp2 = tmp2.next
    l3 = solution2.addTwoNumbers(solution1, solution2)
    while l3:
        print(l3.val)
        l3 = l3.next
